package com.conduit.reporter.server;

import java.io.BufferedWriter;
import java.io.FileWriter;

import org.json.JSONException;
import org.json.JSONObject;

public class ConduitReportingServer implements IDataCallback {

	private Thread serverThread;
	private static boolean continueRunning = true;

	@Override
	synchronized public JSONObject dataReceived(String data) throws JSONException {
		try {
			FileWriter fstream = new FileWriter("c:\\reportingServer.txt",true);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(data);
			out.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
		return new JSONObject().put("Result", "Done");
	}

	public void executeServer() throws InterruptedException {
		TcpServer server = new TcpServer();
		server.addTestListener(this);
		serverThread = new Thread(server);
		serverThread.start();

	while (continueRunning && server.isRunning()) {
			Thread.sleep(1000);
		}
	}

}
